package com.game.common.utils;


import org.springframework.util.StringUtils;

import java.io.UnsupportedEncodingException;

/**
 * 字符串拼接格式化类
 * @author 不愿透露
 */
public class PushUtil {


    public static void main(String[] args) {
        String jianpin = toJianPin("成都");
        System.out.println(jianpin);
        String[] prefix = {"c_","f_", "o_", "s_","m_","v_","h_"};//去掉字符串前缀为pre_、log_
        String str = "sys_content";//测试字符串
        System.out.println(removePrefix(str,prefix));
        System.out.println(splitName("成都高新区芳草社区卫生服务中心",0.3));
        if("区芳草社区卫生服务".contains("社区卫生服务")) {
            System.out.println("asd");
        }
    }

    /**
     *截取字符串前30%后30%保留中间 字数小于6位直接返回
     * @author 不愿透露
     * @date 2021/12/17 21:48
     * @param name
     * @param m
     * @return java.lang.String
     */
    public static String splitName(String name,double m){
        int nameLength = name.length();
        if(nameLength < 10) {
            return name;
        }
        m = m == 0 ? 0.3 : m;
        int length = (int)(nameLength*m);
        double m1 =m -0.1;
        int length1 = (int)(nameLength*m1);
        System.out.println(length);
        return name.substring(length,name.length()-length1);
    }

    /**
     * 去掉字符串指定的前缀
     * @param str 字符串名称
     * @param prefix 前缀数组
     * @return
     */
    public static String removePrefix(String str, String[] prefix) {
        if (StringUtils.isEmpty(str)) {
            return "";
        } else {
            if (null != prefix) {
                String[] prefixArray = prefix;

                for(int i = 0; i < prefix.length; ++i) {
                    String pf = prefixArray[i];
                    if (str.toLowerCase().matches("^" + pf.toLowerCase() + ".*")) {
                        return str.substring(pf.length());//截取前缀后面的字符串
                    }
                }
            }

            return str;
        }
    }



    /**
     *
     * toJP方法：汉字转成简拼 <br/>
     * <br/>
     *
     * @param c
     * @return
     * @exception
     */
    public static String toJianPin(String c) {

        char[] chars = c.toCharArray();

        StringBuffer sb = new StringBuffer("");

        for (int i = 0; i < chars.length; i++) {
            sb.append(getJP(chars[i]));
        }
        String aa = sb.toString();
        return aa;
    }
    public static String getJP(char c) {
        byte[] array = new byte[2];
        try {
            array = String.valueOf(c).getBytes("gbk");
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        if (array.length < 2)
            return String.valueOf(c);
        int i = (short) (array[0] + 256) * 256 + ((short) (array[1] + 256));
        if (i < 0xB0A1)
            return String.valueOf(c);
        if (i < 0xB0C5)
            return "a";
        if (i < 0xB2C1)
            return "b";
        if (i < 0xB4EE)
            return "c";
        if (i < 0xB6EA)
            return "d";
        if (i < 0xB7A2)
            return "e";
        if (i < 0xB8C1)
            return "f";
        if (i < 0xB9FE)
            return "g";
        if (i < 0xBBF7)
            return "h";
        if (i < 0xBFA6)
            return "j";
        if (i < 0xC0AC)
            return "k";
        if (i < 0xC2E8)
            return "l";
        if (i < 0xC4C3)
            return "m";
        if (i < 0xC5B6)
            return "n";
        if (i < 0xC5BE)
            return "o";
        if (i < 0xC6DA)
            return "p";
        if (i < 0xC8BB)
            return "q";
        if (i < 0xC8F6)
            return "r";
        if (i < 0xCBFA)
            return "s";
        if (i < 0xCDDA)
            return "t";
        if (i < 0xCEF4)
            return "w";
        if (i < 0xD1B9)
            return "x";
        if (i < 0xD4D1)
            return "y";
        if (i < 0xD7FA)
            return "z";
        return String.valueOf(c);

    }
}
